/**
 * @Author buguoliang
 * @Date 2019/02/21 19:31
 */
var _bglMall             = require('util/bglMall.js');
var _cities              = require('util/cities/index.js');
var _address             = require('service/address-service.js');
var templateAddressModal = require('./address-modal.string');

var addressModal = {
    show : function (option) {
        this.option      = option;
        this.option.data = option.data || {};
        this.$modalWrap  = $('.modal-wrap');
        // 渲染地址页面
        this.loadModal();
        //绑定事件
        this.bindEvent();
    },
    loadModal : function () {
        var addressModalHtml = _bglMall.renderHtml(templateAddressModal, {
            isUpdate    : this.option.isUpdate,
            data        : this.option.data
        });
        this.$modalWrap.html(addressModalHtml);
        //加载省份
        this.loadProvince();
    },
    loadProvince : function () {
        var provinces       = _cities.getProvinces() || [],
            $provinceSelect = this.$modalWrap.find('#receiver-province');
        $provinceSelect.html(this.getSelectOption(provinces));
        if (this.option.isUpdate && this.option.data.receiverProvince) {
            $provinceSelect.val(this.option.data.receiverProvince);
            //加载城市
            this.loadCities(this.option.data.receiverProvince);
        }
    },
    loadCities : function (provinceName) {
        var cities = _cities.getCities(provinceName);
        var $citySelect = this.$modalWrap.find('#receiver-city');
        $citySelect.html(this.getSelectOption(cities));
        if (this.option.isUpdate && this.option.data.receiverCity) {
            $citySelect.val(this.option.data.receiverCity);
        }
    },
    getSelectOption : function (optionArray) {
        var html = '<option value="">请选择</option>';
        for (var i = 0, iLength = optionArray.length; i < iLength; i++) {
            html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
        }
        return html;
    },
    bindEvent : function () {
        var _this = this;
        //省份，城市二级联动
        this.$modalWrap.find('#receiver-province').change(function () {
            var selectedProvince = $(this).val();
            _this.loadCities(selectedProvince);
        });
        //提交收货地址
        this.$modalWrap.find('.address-btn').click(function () {
            var receiverInfo = _this.getReceiverInfo();
            var isUpdate     = _this.option.isUpdate;
            // 使用新地址且验证 通过
            if (!isUpdate && receiverInfo.status) {
                console.log('1234');
                _address.save(receiverInfo.data, function (res) {
                    console.log('1234567');
                    _bglMall.successTips('地址添加成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function (errMsg) {
                    _bglMall.errorTips(errMsg);
                    console.log('123456789');
                });
                console.log('222334');
            }
            //更新地址且验证通过
            else if (isUpdate && receiverInfo.status) {
                _address.update(receiverInfo.data, function (res) {
                    _bglMall.successTips('地址更新成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function (errMsg) {
                    _bglMall.errorTips(errMsg);
                });
            }
            //验证失败
            else {
                _bglMall.errorTips(receiverInfo.errMsg || '亲,好像哪里不对了哦！');
            }
        });
        // 保证点击modal内容区的时候，不关闭弹窗
        this.$modalWrap.find('.modal-container').click(function (e) {
            e.stopPropagation();
        });
        // 点击叉号或者蒙蔽区域，关闭弹窗
        this.$modalWrap.find('.close').click(function () {
            _this.hide();
        });
    },
    //获取表单里收件人的信息，并做验证
    getReceiverInfo : function () {
        var receiverInfo = {},
            result       = {
                status : false
            };
        receiverInfo.receiverName       = $.trim(this.$modalWrap.find('#receiver-name').val());
        receiverInfo.receiverProvince   = this.$modalWrap.find('#receiver-province').val();
        receiverInfo.receiverCity       = this.$modalWrap.find('#receiver-city').val();
        receiverInfo.receiverAddress    = $.trim(this.$modalWrap.find('#receiver-address').val());
        receiverInfo.receiverPhone      = $.trim(this.$modalWrap.find('#receiver-phone').val());
        receiverInfo.receiverZip        = $.trim(this.$modalWrap.find('#receiver-zip').val());
        if (this.option.isUpdate) {
            receiverInfo.id = this.$modalWrap.find('#receiver-id').val();
        }
        // 表单验证
        if (!receiverInfo.receiverName) {
            result.errMsg = '请输入收件人姓名';
        } else if (!receiverInfo.receiverProvince) {
            result.errMsg = '请输入收件人省份';
        } else if (!receiverInfo.receiverCity) {
            result.errMsg = '请输入收件人城市';
        } else if (!receiverInfo.receiverAddress) {
            result.errMsg = '请输入收件人详细地址';
        } else {
            //手机号单独校验
            if (!receiverInfo.receiverPhone) {
                result.errMsg = '请输入收件人手机号';
                return result;
            } else {
                if (!(/^1(3|4|5|7|8)\d{9}$/.test(receiverInfo.receiverPhone))){
                    result.errMsg = '手机号码格式有误，请重新输入';
                    return result;
                }
            }
            result.status = true;
            result.data   = receiverInfo;
        }
        return result;
    },
    hide : function () {
        this.$modalWrap.empty();
    }
};

module.exports = addressModal;